import { datas } from './fields.js'
let that
// 串口监听
export default {
	data() {
		return {
			deviceInfo: {},
			DateValue: "请选择日期",
			expectTimeHi: "请选择时间",
			adminList: [],
			selectedItem: '请选择',
			formData: {
				borrowAdminId: '',
				boxId: '',
				deviceId: '',
				expectTime: '',
				expectTimeHi: '',
				userId: '',
			},
			deviceInfo: {}
		}
	},
	computed: {},
	onShow() {
		that = this
		that.deviceInfo = uni.getStorageSync("deviceInfo")
		uni.request({
			url: '/api/h5/getAdminList',
			method: "POST",
			header: {},
			data: {},
			success: (res) => {
				that.adminList = res.data.data
			},
			fail: (err) => {
				console.error(err);
			}
		});
	},
	created() {
		that = this
	},
	onReady() {},
	onLoad(options) {
		that = this
		console.log(options)
	},
	methods: {
		onDateChange: function(e) {
			this.DateValue = e.detail.value;
			this.formData.expectTime = e.detail.value
		},
		onDateChanges: function(e) {
			this.expectTimeHi = e.detail.value;
			this.formData.expectTimeHi = e.detail.value
		},
		onPickerChange(e) {
			this.selectedItem = this.adminList[e.detail.value]['userName']
			this.formData.borrowAdminId = this.adminList[e.detail.value]['id']
		},
		submitInfo() {
			if(that.formData.expectTime == '' || that.formData.expectTimeHi == ''){
				uni.showToast({
					title: '请选择预计归还时间',//标题 必填
					icon: 'none',
				})
				return;
			}
			if(that.formData.borrowAdminId == ''){
				uni.showToast({
					title: '请选择借用审批人',//标题 必填
					icon: 'none',
				})
				return;
			}
			let subData = {
				borrowAdminId: this.formData.borrowAdminId,
				boxId: this.deviceInfo.boxId,
				deviceId: this.deviceInfo.id,
				expectTime: this.formData.expectTime + ' ' + that.formData.expectTimeHi + ':00',
				userId: uni.getStorageSync("userId")
			}
			uni.request({
				url: '/api/h5/deviceBorrowApply',
				method: "POST",
				header: {},
				data: subData,
				success: (res) => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '仪器借用申请成功',//标题 必填
							icon: 'none',//图标
						    mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: '1500',//提示的延迟时间，单位毫秒，默认：1500
						    success: '',//接口调用成功的回调函数	
						    fail: '',//接口调用失败的回调函数	
						    complete: function(res){},//接口调用结束的回调函数（调用成功、失败都会执行）
						});
						setTimeout(()=>{
							uni.navigateBack()
						},1500)
						
					} else {
						uni.showToast({
							title: '仪器借用申请失败',//标题 必填
							icon: 'none',//图标
						    mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: '1500',//提示的延迟时间，单位毫秒，默认：1500
						    success: '',//接口调用成功的回调函数	
						    fail: '',//接口调用失败的回调函数	
						    complete: function(res){},//接口调用结束的回调函数（调用成功、失败都会执行）
						});
						
					}
				},
				fail: (err) => {
					console.error(err);
				}
			});

		}
	}
}